package phapp.service.custom.impl;

import java.util.*;

import net.sf.json.*;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import phapp.model.custom.*;
import phapp.model.system.Version;
import phapp.dao.custom.*;
import phapp.service.common.QfdException;
import phapp.service.custom.impl.*;
import phapp.service.custom.*;

/**
 * Title: t_custom_favorite 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("customFavoriteManager")
@Transactional
public class CustomFavoriteManagerImpl extends BaseManager<CustomFavorite, java.lang.Integer> implements CustomFavoriteManager
{

   /** 定义Dao */
   private CustomFavoriteDao customFavoriteDao;
   private CustomManager customManager;

   /** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
   public void setCustomFavoriteDao(CustomFavoriteDao dao)
   {
      this.customFavoriteDao = dao;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.customFavoriteDao;
   }

   public void setCustomManager(CustomManager customManager)
   {
      this.customManager = customManager;
   }

   /**
    * <p>
    * 分页查询CustomFavorite<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return customFavoriteDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 CustomFavorite<br>
    * 
    * @param CustomFavorite
    *           u
    * @return Boolean
    * @author
    */
   @Transactional(readOnly = true)
   public Boolean removeByIdArry(CustomFavorite u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = customFavoriteDao.removeByIdArry(u);
      if (i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }

   public Map collectData(JSONObject jsonData)
   {
      int nStatus = 0;
      String CustAccount = "";
      Custom customData = null;
      CustomFavorite favoriteData = null;
      List<Custom> listQuery = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         
         customData = listQuery.get(0);
         favoriteData = new CustomFavorite();
         favoriteData.setCustId(customData.getId());
         favoriteData.setLuckFortune(jsonData.getString("fortune"));
         favoriteData.setLuckArticle(jsonData.getString("luckcolor"));
         favoriteData.setLuckColor(jsonData.getString("luckarticle"));
         favoriteData.setLuckPos(jsonData.getString("luckpos"));
         favoriteData.setLuckValue(new Integer(jsonData.getString("luckvalue")));
         favoriteData.setConstellation(jsonData.getString("constellation"));
         
         save(favoriteData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "收藏成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else
            mapResult.put("message", "收藏失败！");
      }
      
      return mapResult;
   }
   
   public Map getCollectData(JSONObject jsonData)
   {
      int nStatus = 0, i = 0;
      String CustAccount = "";
      Custom customData = null;
      JSONArray arrayData = null;
      JSONObject jsonFavorite = null;
      CustomFavorite favoriteData = null;
      List<Custom> listQuery = null;
      List<CustomFavorite> listData = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         
         customData = listQuery.get(0);
         mapQuery.clear();
         mapQuery.put("custId", customData.getId());
         listData = customFavoriteDao.findList(mapQuery);
         
         arrayData = new JSONArray();
         for(i=0; i<listData.size(); i++)
         {
            favoriteData = listData.get(i);
            jsonFavorite = new JSONObject();
            jsonFavorite.put("luckvalue", favoriteData.getLuckValue());
            jsonFavorite.put("constellation", favoriteData.getConstellation());
            jsonFavorite.put("fortune", favoriteData.getLuckFortune());
            jsonFavorite.put("luckcolor", favoriteData.getLuckColor());
            jsonFavorite.put("luckarticle", favoriteData.getLuckArticle());
            jsonFavorite.put("luckpos", favoriteData.getLuckPos());
            jsonFavorite.put("favdate", favoriteData.getFavDateString());
            
            arrayData.add(jsonFavorite);
            jsonFavorite = null;
         }
         
         mapResult.put("favlist", arrayData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2004)
            mapResult.put("message", "密码错误！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }
   
   
}
